// config-overrides.js
/* eslint-disable no-useless-computed-key */
const {
    override,
    addWebpackAlias,
    addWebpackResolve,
    fixBabelImports,
    addLessLoader,
    addBabelPlugins,
    addWebpackPlugin,
    addWebpackModuleRule,
    removeModuleScopePlugin,
} = require('customize-cra');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const path = require('path');
console.log(process.env.REACT_APP_NODE_ENV);
const threadLoader = require('thread-loader');
threadLoader.warmup(
    {
        // pool options, like passed to loader options
        // must match loader options to boot the correct pool
    },
    [
        // modules to load
        // can be any module, i. e.
        'babel-loader',
    ]
);
const isProd = process.env.REACT_APP_NODE_ENV === 'prod' || process.env.REACT_APP_NODE_ENV === 'test';
module.exports = override(
    addWebpackResolve({
        extensions: ['.tsx', '.ts', '.js', '.jsx', '.json'],
    }),
    addWebpackAlias({
        ['@']: path.resolve(__dirname, 'src'),
    }),
    fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: 'less',
    }),
    addLessLoader({
        strictMath: true,
        noIeCompat: true,
        // javascriptEnabled: true,
        cssLoaderOptions: {}, // .less file used css-loader option, not all CSS file.
        cssModules: {
            localIdentName: '[path][name]__[local]--[hash:base64:5]', // if you use CSS Modules, and custom `localIdentName`, default is '[local]--[hash:base64:5]'.
        },
    }),
    isProd &&
        addWebpackPlugin(
            new UglifyJsPlugin({
                uglifyOptions: {
                    compress: {
                        drop_debugger: true,
                        drop_console: true,
                    },
                },
            })
        ) &&
        addBabelPlugins([
            'transform-remove-console',
            {
                exclude: ['error', 'warn'], // 可选，指定不删除的 console 方法
            },
        ]),
    addWebpackPlugin(new NodePolyfillPlugin()),
    process.env.REACT_APP_ANALYZER === 'true' && addWebpackPlugin(new BundleAnalyzerPlugin()),
    addWebpackPlugin(new ProgressBarPlugin()),
    isProd &&
        addWebpackPlugin(
            new CompressionPlugin({
                filename: '[path][base].gz',
                algorithm: 'gzip',
                test: /\.js$|\.html$|\.css/,
                threshold: 1024,
                minRatio: 1,
                deleteOriginalAssets: false,
            })
        ),
    addWebpackModuleRule({
        test: /\.(sa|sc|c)ss$/,
        use: [
            isProd
                ? {
                      loader: MiniCssExtractPlugin.loader,
                      options: {
                          publicPath: './',
                      },
                  }
                : 'style-loader',
            'css-loader',
            'sass-loader',
        ],
    }),
    // addWebpackModuleRule({
    //     test: /\.(j|t)s$/,
    //     include: path.resolve('src'),
    //     use: [
    //         {
    //             loader: 'thread-loader',
    //         },
    //         {
    //             loader: 'ts-loader',
    //             options: {
    //                 happyPackMode: true,
    //             },
    //         },
    //     ],
    // }),
    isProd && addWebpackPlugin(new MiniCssExtractPlugin()),
    config => {
        if (isProd) {
            config.devtool = false;
        }
        return config;
    },
    removeModuleScopePlugin()
);
